/**
 * 
 */
package com.pkdl.forms;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import com.pkdl.dao.BenhNhanChoKhamDAO;
import com.pkdl.dao.BenhNhanDAO;
import com.pkdl.dto.BenhNhanChoKhamDTO;
import com.pkdl.dto.BenhNhanDTO;
import com.pkdl.enums.TinhTrangKham;
import com.pkdl.helpers.Converter;
import com.pkdl.helpers.SorterByName;

/**
 * @author Administrator
 * 
 */
public class DlgTiepNhanBenhNhan extends JDialog {
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgTiepNhanBenhNhan dialog = new DlgTiepNhanBenhNhan(null, true);
			dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private BenhNhanDAO bnDAO;
	private BenhNhanChoKhamDAO bnckDAO;
	private JTextField txtThoiDiemTiepNhan;
	private JComboBox cbxBenhNhan;

	/**
	 * Create the dialog.
	 */
	public DlgTiepNhanBenhNhan(Frame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Tiep nhan benh nhan");
		this.setBounds(100, 100, 484, 211);
		this.setLocationRelativeTo(null);
		this.getContentPane().setLayout(new BorderLayout());

		JPanel panel = new JPanel();
		panel.setBackground(SystemColor.textHighlight);
		this.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));

		JLabel lblTipNhnBnh = new JLabel(
				"TI\u1EBEP NH\u1EACN B\u1EC6NH NH\u00C2N");
		lblTipNhnBnh.setPreferredSize(new Dimension(31, 32));
		lblTipNhnBnh.setHorizontalAlignment(SwingConstants.CENTER);
		lblTipNhnBnh.setForeground(Color.WHITE);
		lblTipNhnBnh.setFont(new Font("Tahoma", Font.BOLD, 18));
		panel.add(lblTipNhnBnh, BorderLayout.NORTH);

		JPanel panel_1 = new JPanel();
		panel_1.setLayout(null);
		this.getContentPane().add(panel_1, BorderLayout.CENTER);

		JLabel lblMBnhNhn = new JLabel("B\u1EC7nh nh\u00E2n");
		lblMBnhNhn.setBounds(24, 25, 89, 14);
		panel_1.add(lblMBnhNhn);

		JLabel lblThiimTip = new JLabel(
				"Th\u1EDDi \u0111i\u1EC3m ti\u1EBFp nh\u1EADn");
		lblThiimTip.setBounds(24, 53, 114, 14);
		panel_1.add(lblThiimTip);

		this.txtThoiDiemTiepNhan = new JTextField();
		this.txtThoiDiemTiepNhan.setEditable(false);
		this.txtThoiDiemTiepNhan.setColumns(10);
		this.txtThoiDiemTiepNhan.setBounds(148, 50, 272, 20);
		this.txtThoiDiemTiepNhan.setText(new SimpleDateFormat(
				"dd/MM/yyyy HH:mm:ss").format(new Date()));
		panel_1.add(this.txtThoiDiemTiepNhan);

		JButton btnTiepNhan = new JButton("Ti\u1EBFp nh\u1EADn");
		btnTiepNhan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgTiepNhanBenhNhan.this.tiepNhanBenhNhan();
			}
		});
		btnTiepNhan.setIcon(new ImageIcon(DlgTiepNhanBenhNhan.class
				.getResource("/com/pkdl/resources/001_01.gif")));
		btnTiepNhan.setBounds(293, 94, 127, 23);
		panel_1.add(btnTiepNhan);

		this.cbxBenhNhan = new JComboBox();
		this.cbxBenhNhan.setBounds(148, 22, 272, 20);
		panel_1.add(this.cbxBenhNhan);

		JButton btnChon = new JButton("");
		btnChon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DlgQuanLyBenhNhan dlg = new DlgQuanLyBenhNhan(null, true);
				dlg.show();
				DlgTiepNhanBenhNhan.this.loadData();
			}
		});
		btnChon.setIcon(new ImageIcon(DlgTiepNhanBenhNhan.class
				.getResource("/com/pkdl/resources/001_03.gif")));
		btnChon.setBounds(430, 21, 26, 23);
		panel_1.add(btnChon);

		// Load danh sach benh nhan
		this.loadData();
	}

	/**
	 * Kiem tra benh nhan da ton tai trong danh sach cho kham hay chua
	 */
	private boolean isExistentInDanhSachChoKham(int maBenhNhan) {
		this.bnckDAO = new BenhNhanChoKhamDAO();
		// Lay danh sach benh nhan cho kham
		List<BenhNhanChoKhamDTO> lstItem = this.bnckDAO.selectAll();
		for (BenhNhanChoKhamDTO item : lstItem) {
			// Benh nhan duoc lua chon da co trong danh sach benh nhan cho kham
			if (maBenhNhan == item.getMaBenhNhan()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Load danh sach benh nhan
	 */
	private void loadData() {
		this.cbxBenhNhan.removeAllItems();
		this.bnDAO = new BenhNhanDAO();
		List<BenhNhanDTO> ds = this.bnDAO.selectAll();
		Collections.sort(ds, new SorterByName());
		String[] arr = Converter.convertListToArray_BenhNhan(ds);
		for (String element : arr) {
			this.cbxBenhNhan.addItem(element);
		}
	}

	/**
	 * Them benh nhan vao danh sach cho kham
	 */
	private void tiepNhanBenhNhan() {
		this.bnckDAO = new BenhNhanChoKhamDAO();
		String selectedBenhNhan = (String) this.cbxBenhNhan.getSelectedItem();
		String[] compBenhNhan = selectedBenhNhan.split("-");
		String maBenhNhan = compBenhNhan[0].trim();
		BenhNhanChoKhamDTO item = new BenhNhanChoKhamDTO(
				Integer.parseInt(maBenhNhan), new Timestamp(
						new Date().getTime()),
				TinhTrangKham.DANGDOIKHAM.toString());
		// Da ton tai benh nhan trong danh sach
		if (this.isExistentInDanhSachChoKham(item.getMaBenhNhan())) {
			JOptionPane.showMessageDialog(null,
					"Benh nhan duoc lua chon da co trong danh sach",
					"Announcement", JOptionPane.WARNING_MESSAGE);
		} else {
			// Chua ton tai, thuc hien insert
			if (this.bnckDAO.insert(item)) {
				JOptionPane.showMessageDialog(null, "Them du lieu thanh cong",
						"Announcement", JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "Khong the them du lieu",
						"Announcement", JOptionPane.ERROR_MESSAGE);
			}
		}

	}
}
